from django.db.models import Q
from rest_framework import filters

from apps.users.models import Provider


def provider_filter_queryset(request, queryset):
        params = request.query_params
        query = queryset

        if 'provider_type' in params:
            provider_type = eval(params['provider_type'])
            query = query.filter(provider_license__license_type__in=provider_type).distinct()

        if 'status' in params:
            status = params['status']
            query = query.filter(status=status).distinct()

        if 'search' in params:
            search = params['search']
            query = query.filter(Q(first_name__icontains=search) | Q(last_name__icontains=search) | Q(email__icontains=search))

        return query

